#pragma once

#include "NonCopyable.hh"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>

namespace wd
{

class MutexLock
    : public Noncopyable
{
  public:
    MutexLock()
    {
        if (pthread_mutex_init(&_mutex, nullptr)) {
            perror("pthread_mutex_init");
        }
    }
    ~MutexLock()
    {
        if (pthread_mutex_destroy(&_mutex)) {
            perror("pthread_mutex_destory");
        }
    }
    void lock()
    {
        if (pthread_mutex_lock(&_mutex)) {
            perror("pthread_mutex_lock");
        }
    }
    void unlock()
    {
        if (pthread_mutex_unlock(&_mutex)) {
            perror("pthread_mutex_unlock");
        }
    }
    pthread_mutex_t *getMutexPtr()
    {
        return &_mutex;
    }

  private:
    pthread_mutex_t _mutex;
};

class MutexLockGuard
{
  public:
    MutexLockGuard(MutexLock &mutex)
        : _mutex(mutex)
    {
        _mutex.lock();
    }

    ~MutexLockGuard()
    {
        _mutex.unlock();
    }

  private:
    MutexLock &_mutex;
};

} // end of namespace wd